package com.cskaoyan.mall.controller.mall;

import com.cskaoyan.mall.model.Result;

import com.cskaoyan.mall.model.bo.admin.AdminLoginBO;
import com.cskaoyan.mall.model.bo.admin.UpdateUserDataBO;
import com.cskaoyan.mall.model.bo.mall.SignUpBO;

import com.cskaoyan.mall.model.bo.mall.UpdatePwdBO;
import com.cskaoyan.mall.model.bo.mall.UserLoginBO;
import com.cskaoyan.mall.model.vo.admin.AdminLoginVO;
import com.cskaoyan.mall.model.vo.mall.GetTokenVO;
import com.cskaoyan.mall.model.vo.mall.GetUserIdVO;
import com.cskaoyan.mall.model.vo.mall.SignUpVO;

import com.cskaoyan.mall.model.vo.mall.UserDataVO;
import com.cskaoyan.mall.service.mall.MallUserService;
import com.cskaoyan.mall.service.mall.MallUserServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.cskaoyan.mall.utils.StringUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/api/mall/user/*")
public class MallUserServlet extends HttpServlet {
    ///api/mall/user/signup
    private Gson gson = new Gson();
    private MallUserService mallUserService = new MallUserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("dopost");
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/user/","");
        //api/mall/user/signup
        if ("signup".equals(action)) {
            signup(request,response);
        }else if ("login".equals(action)) {
            login(request,response);
        }else if ("updatePwd".equals(action)) {
            updatePwd(request,response);
        }else if ("updateUserData".equals(action)) {
            updateUserData(request,response);
        }else if ("logoutClient".equals(action)) {
            logoutClient(request,response);
        }
    }

    /**
     * 用户退出
     * @param request
     * @param response
     */
    private void logoutClient(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GetTokenVO data= gson.fromJson(requestBody, GetTokenVO.class);
        int code = mallUserService.logoutClient(data);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("退出失败")));
    }

    /**
     * 更改用户资料
     * @param request
     * @param response
     */
    private void updateUserData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        UpdateUserDataBO data= gson.fromJson(requestBody,UpdateUserDataBO.class);
        int code = mallUserService.updateUserData(data);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("修改失败")));
    }

    /**
     * 用户界面修改密码
     * @param request
     * @param response
     */
    private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        UpdatePwdBO pwd = gson.fromJson(requestBody,UpdatePwdBO.class);
        if (!pwd.getNewPwd().equals(pwd.getConfirmPwd())) {
            response.getWriter().println(Result.error("新密码与确认密码不一致"));
        }
        int code = mallUserService.updatePwd(pwd);
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("请确认密码")));
    }

    /**
     * 用户登录
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        //BO用来处理客户端的输入，这里对请求报文进行处理
        UserLoginBO loginBO = gson.fromJson(requestBody,UserLoginBO.class);
        if (StringUtils.isEmpty(loginBO.getEmail())|| StringUtils.isEmpty(loginBO.getPwd())) {
            //返回响应结果
            response.getWriter().println(gson.toJson(Result.error("参数不能为空")));
            return;
        }
        int code = mallUserService.login(loginBO);
        if (code == 200) {
            request.getSession().setAttribute("username",loginBO.getEmail());
            response.getWriter().println(gson.toJson(Result.ok(new AdminLoginVO(loginBO.getEmail(),loginBO.getEmail()))));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("请确认用户名和密码")));
    }

    /**
     * 用户注册
     * @param request
     * @param response
     */
    private void signup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("signup");
        String requestBody = HttpUtils.getRequestBody(request);
        //对请求报文进行处理
        SignUpBO signUpBO = gson.fromJson(requestBody,SignUpBO.class);
        if (StringUtils.isEmpty(signUpBO.getEmail()) ||
                StringUtils.isEmpty(signUpBO.getNickname()) ||
                StringUtils.isEmpty(signUpBO.getPwd()) ||
                StringUtils.isEmpty(signUpBO.getPhone()) ||
                StringUtils.isEmpty(signUpBO.getAddress()) ||
                StringUtils.isEmpty(signUpBO.getRecipient())) {
            response.getWriter().println(gson.toJson(Result.error("输入的参数不能为空")));
            return;
        }
        int code = mallUserService.signup(signUpBO);;
        if (code == 200) {
            request.getSession().setAttribute("username",signUpBO.getNickname());
            response.getWriter().println(gson.toJson(Result.ok(new SignUpVO(0,signUpBO.getNickname(),signUpBO.getNickname()))));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("注册失败")));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/user/", "");
        if("data".equals(action)){
            data(request, response);
        }
    }

    /**
     * 返回用户的信息
     * //        {"code":0,
     * //        "data":{
     * //        "code":0,
     * //        "id":"617",
     * //        "email":"1511240@qq.com",
     * //        "nickname":"Sandy",
     * //        "recipient":"曾钰",
     * //        "address":"北京市朝阳区",
     * //        "phone":"13576608973"}}
     * @param request
     * @param response
     */
    private void data(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        UserDataVO user = mallUserService.data(token);
        response.getWriter().println(gson.toJson(Result.ok(user)));
    }
}
